package com.jz.banner.screen.utils

import android.content.Context
import android.content.SharedPreferences
import com.jz.banner.screen.ui.App


/**
 * 创建者     Noel
 * 创建时间   2017/12/22 10:33
 */
object SPUtils {
    const val CONFIG = "CONFIG"
    const val FIRST_CONFIG = "IS_FIRST"

    //private val HISTORY: String by lazy { sp.getString(Params.userName, "HISTORY") }
    //private val HISTORY = SPUtils.putShopsId(1)
    // private val REMEMBER_ACCOUNT = "REMEMBER_ACCOUNT"
    private var sp: SharedPreferences = App.context!!.getSharedPreferences(CONFIG, Context.MODE_PRIVATE)

    /*app logo*/
    fun putAppLogo(url: String?) {
        sp.edit()
            .putString("AppLogo", url)
            .apply()
    }

    fun getAppLogo(): String {
        return sp.getString("AppLogo", null) ?: Urls.LOGO_URL_APP
    }

    /*白色logo*/
    fun putLogoWhite(url: String?) {
        sp.edit()
            .putString("LogoWhite", url)
            .apply()
    }

    fun getLogoWhite(): String {
        return sp.getString("LogoWhite", null) ?: Urls.LOGO_URL_WHITE
    }

    /*蓝色logo*/
    fun putLogoBlue(url: String?) {
        sp.edit()
            .putString("LogoBlue", url)
            .apply()
    }

    fun getLogoBlue(): String {
        return sp.getString("LogoBlue", null) ?: Urls.LOGO_URL_BLUE
    }


    fun putString(key: String, value: String?) {
        sp.edit()
            .putString(key, value)
            .apply()
    }

    fun getString(key: String): String? {
        return sp.getString(key, null)
    }

    fun putInt(key: String, value: Int) {
        sp.edit()
            .putInt(key, value)
            .apply()
    }

    fun getInt(key: String, int: Int): Int {
        return sp.getInt(key, int)
    }

    fun putFloat(key: String, value: Float) {
        try {
            sp.edit()
                .putFloat(key, value)
                .apply()
        } catch (e: Exception) {

        }
    }

    fun getFloat(key: String, float: Float): Float {
        return try {
            sp.getFloat(key, float)
        } catch (e: Exception) {
            float
        }
    }

    fun putBoolean(key: String, value: Boolean, spName: String? = null) {
        if (spName.isNullOrBlank()) {
            sp.edit()
                .putBoolean(key, value)
                .apply()
        } else {
            App.context!!.getSharedPreferences(spName, Context.MODE_PRIVATE)
                .edit()
                .putBoolean(key, value)
                .apply()
        }
    }

    //第一次启动安装
    fun isFirstRun(): Boolean {
        var sharedPreferences = App.context!!.getSharedPreferences(FIRST_CONFIG, Context.MODE_PRIVATE)
        var isFirstRun = sharedPreferences.getBoolean("isFirstRun", true)
        var editor = sharedPreferences.edit()
        return if (isFirstRun) {
            editor.putBoolean("isFirstRun", false)
            editor.apply()
            true
        } else false
    }

    //第一次引导
    fun isFirstGuide(): Boolean {
        var sharedPreferences = App.context!!.getSharedPreferences(FIRST_CONFIG, Context.MODE_PRIVATE)
        var isFirstRun = sharedPreferences.getBoolean("isFirstGuide", true)
        var editor = sharedPreferences.edit()
        return if (isFirstRun) {
            editor.putBoolean("isFirstGuide", false)
            editor.apply()
            true
        } else false
    }


    fun getBoolean(key: String, spName: String? = null, default: Boolean = false): Boolean {
        return if (spName.isNullOrBlank()) sp.getBoolean(key, default) else {
            App.context!!.getSharedPreferences(spName, Context.MODE_PRIVATE)
                .getBoolean(key, default)
        }
    }

    fun putToken(value: String?) {
        sp.edit()
            .putString(Params.future_json, value)
            .apply()
    }

    fun getToken(): String? {
        return sp.getString(Params.future_json, null)
    }

    fun putMobile(value: String?) {
        sp.edit()
            .putString(Params.mobile, value)
            .apply()
    }

    fun getUserId(): String? {
        return sp.getString(Params.user_id, null)
    }

    fun putUserId(value: String?) {
        sp.edit()
            .putString(Params.user_id, value)
            .apply()
    }

    fun getMobile(): String? {
        return sp.getString(Params.mobile, null)
    }

    fun clear() {
        sp.edit()
            .clear()
            .apply()
    }

    fun rememberAccount(tel: String?) {
        var sp = App.context!!.getSharedPreferences(CONFIG, Context.MODE_PRIVATE)
            .edit()
        sp.putString(Params.tel, tel)
        sp.apply()
    }

    fun getAccountTel(): String? {
        var sp = App.context!!.getSharedPreferences(CONFIG, Context.MODE_PRIVATE)
        return sp.getString(Params.tel, null)
    }

    fun putPassword(pwd: String? = null) {
        var sp = App.context!!.getSharedPreferences(CONFIG, Context.MODE_PRIVATE)
            .edit()
        sp.putString(Params.password, pwd)
        sp.apply()
    }

    fun getAccountPwd(): String? {
        var sp = App.context!!.getSharedPreferences(CONFIG, Context.MODE_PRIVATE)
        return sp.getString(Params.password, null)
    }

}


